import urllib.request
from bs4 import BeautifulSoup
from selenium import webdriver
import json
import random
import time
import ssl
import re
ssl._create_default_https_context = ssl._create_unverified_context
class poppro_spider():
    def __init__(self):
        self.driver = webdriver.PhantomJS()    # 创建一个浏览器驱动
        self.start_url = "http://poppro.cn/html/list-music.html"  # 歌曲首页链接
        self.driver.get(self.start_url)
        self.download_url = "https://www.poppro.cn/work/download?"    #下载链接
        self.i = 0  # 计数
        self.song_message = {}

    def get_main_page(self):    #获取歌曲首页的源码
        main_html = self.driver.page_source      #获取歌曲首页的源代码
        # print(main_html)
        # self.driver.save_screenshot("screenshot/main_page.png")
        return main_html

    def get_song_message(self, main_html):   #获取首页上每首歌的信息（名字和下载链接）
        soup = BeautifulSoup(main_html)   #创建一个beautifulsoup对象
        song_tag_list = soup.select('li[class="col-1"] a[target="_blank"]')   # 获取信息所在的标签

        for tag in song_tag_list:
            self.i += 1
            song_name = tag.get_text()        # 歌曲名
            song_url = "http://poppro.cn/html" + tag["href"][1:]    # 歌曲完整的链接
            type_and_id = "wId" + song_url[-12:]  # 获取type和id
            song_download_url = self.download_url + type_and_id  # 歌曲下载链接
            self.song_message[self.i] = {"name" : song_name, "download_url": song_download_url}

        return self.song_message #返回字典

    def get_ua(self): # 获取一个随机的user-agent
        ua_list = [
            "Mozilla/5.0(Macintosh;U;IntelMacOSX10_6_8;en-us)AppleWebKit/534.50(KHTML,likeGecko)Version/5.1Safari/534.50",
            "Mozilla/5.0(Windows;U;WindowsNT6.1;en-us)AppleWebKit/534.50(KHTML,likeGecko)Version/5.1Safari/534.50",
            "Mozilla/5.0(compatible;MSIE9.0;WindowsNT6.1;Trident/5.0;",
            "Mozilla/4.0(compatible;MSIE8.0;WindowsNT6.0;Trident/4.0)",
            "Mozilla/4.0(compatible;MSIE7.0;WindowsNT6.0)",
            "Mozilla/4.0(compatible;MSIE6.0;WindowsNT5.1)",
            "Mozilla/5.0(Macintosh;IntelMacOSX10.6;rv:2.0.1)Gecko/20100101Firefox/4.0.1"
        ]
        user_agent = {
            "User-Agent": random.choice(ua_list)
        }
        return user_agent

    def download_song(self):  #下载歌曲
        for num, message in self.song_message.items():
            try :
                download_url = message["download_url"]
                song_name = message["name"]
                request = urllib.request.Request(download_url, headers=self.get_ua())
                response = urllib.request.urlopen(request)
                song_bytes = response.read()
                song_name = re.sub(r'[\/:*?"<>|]', " ", song_name)  # 将\ / : * ? " < > |字符替换成空格
                file_name = 'E://myworkplace_(python)/爬虫项目/爬取poppro歌曲/songs/' + song_name + ".mp3"
                if len(song_bytes) < 1000:       # 如果响应的不是歌曲字节，则跳过
                    print("该作者不提供下载：" + song_name)
                    continue
                print("正在下载第%s" % num + "首歌：" + song_name)
                file = open(file_name, "wb")   # 将歌曲存入本地
                file.write(song_bytes)
                file.close()
            except :
                print("下载出错")
                continue


if __name__ == '__main__':
    spider = poppro_spider()   # 创建爬虫对象
    print("获取第1页数据")
    main_html = spider.get_main_page()   # 获取歌曲首页页面源码
    spider.get_song_message(main_html) # 获取每首歌的信息（名字和下载链接）
    c = 0 #记录点击次数
    while True:
        c += 1
        print("获取第%s页数据"%(c+1))
        spider.driver.find_element_by_xpath('//div[@id="pagination"]/div/a[last()-1]').click()
        time.sleep(2)
        main_html = spider.get_main_page()  # 获取歌曲首页页面源码
        spider.get_song_message(main_html)  # 获取每首歌的信息（名字和下载链接）
        if c == 36 :
            spider.driver.save_screenshot("screenshot/last_page.png")
            break

    spider.download_song()       # 下载歌曲







